﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Shared.Method
{
    public class LogHelper
    {

        public const string WcfLogPath = "D:\\WCFLog";

        public static void WriteErrLogTxt(string strFunctionName, string strErrorNum, string strErrorDescription)
        {
            string strMatter;
            string filePath = LogHelper.WcfLogPath;
            DateTime dt = DateTime.Now;
            try
            {
                if (Directory.Exists(filePath) == false)
                {
                    Directory.CreateDirectory(filePath);
                }
                filePath = filePath + "\\" + dt.ToString("yyyyMM");
                if (Directory.Exists(filePath) == false)
                {
                    Directory.CreateDirectory(filePath);
                }
                filePath = filePath + "\\" + dt.ToString("dd") +"_ERROR"+ ".txt";
                strMatter = "<发生错误的方法名：>" + strFunctionName + " , <错误所在位置：>" + strErrorNum + " , <错误信息：>" + strErrorDescription;
                StreamWriter FileWriter = new StreamWriter(filePath, true);
                FileWriter.WriteLine("【时    间：】" + dt.ToString("HH:mm:ss"));
                FileWriter.WriteLine("【错误内容：】" + strMatter);
                FileWriter.WriteLine("========================================================================================================");
                FileWriter.Close();
            }
            catch { return; }
        }

        public static void DebugLog(string strMsg)
        {
            string filePath = LogHelper.WcfLogPath;
            DateTime dt = DateTime.Now;
            try
            {
                if (Directory.Exists(filePath) == false)
                {
                    Directory.CreateDirectory(filePath);
                }
                filePath = filePath + "\\" + "DebugLog";

                if (Directory.Exists(filePath) == false)
                {
                    Directory.CreateDirectory(filePath);
                }
                filePath = filePath + "\\" + "Log.txt";
                StreamWriter FileWriter = new StreamWriter(filePath, true);
                FileWriter.WriteLine("【时    间：】" + dt.ToString("HH:mm:ss"));
                FileWriter.WriteLine("【内    容：】" + strMsg);
                FileWriter.WriteLine("========================================================================================================");
                FileWriter.Close();
            }
            catch
            {
                return;
            }
        }

        public static void WriteDebugLogTxt(string strFlg, string strMsg)
        {
            string strMatter;
            string filePath = LogHelper.WcfLogPath;
            DateTime dt = DateTime.Now;
            try
            {
                if (Directory.Exists(filePath) == false)
                {
                    Directory.CreateDirectory(filePath);
                }

                filePath = filePath + "\\" + dt.ToString("yyyyMM");

                if (Directory.Exists(filePath) == false)
                {
                    Directory.CreateDirectory(filePath);
                }

                filePath = filePath + "\\" + dt.ToString("dd")+"_DEBUG" + ".txt";

                strMatter = "<" + strFlg + ">：" + strMsg;

                StreamWriter FileWriter = new StreamWriter(filePath, true);
                FileWriter.WriteLine("【时    间：】" + dt.ToString("HH:mm:ss"));

                FileWriter.WriteLine("【内    容：】" + strMatter);
                FileWriter.WriteLine("========================================================================================================");
                FileWriter.Close();
            }
            catch
            {
                return;
            }
        }



        public static void WriteLog(string dirPath, string filePath, string strMsg, bool writeLine = false)
        {
            try
            {
                if (Directory.Exists(dirPath) == false)
                {
                    Directory.CreateDirectory(dirPath);
                }

                StreamWriter FileWriter = null;

                if (File.Exists(dirPath + filePath) == false)
                    FileWriter = new StreamWriter(File.Create(dirPath + filePath));
                else
                    FileWriter = new StreamWriter(dirPath + filePath, true);

                if (writeLine)
                    FileWriter.WriteLine(strMsg);
                else
                    FileWriter.Write(strMsg);

                FileWriter.Close();
            }
            catch (Exception ex)
            {
                WriteErrLogTxt("WriteLog", "写日志失败", ex.ToString());
            }
        }

    }
}
